﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TfsUtils.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class XmlUtilsFixture
    {
        public XmlUtilsFixture()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class

        private static String testdir;
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            testdir = Path.GetTempPath();

        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void VerifyFileDoesNotExistsDataIsGenerated()
        {

            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("BLABLABLA", "categoryNew");
            Assert.AreEqual(1, value);
        }

        [TestMethod]
        public void VerifyFileDoesNotExistsNeedToUpdate()
        {

            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("BLABLABLA", "categoryNew");
            Assert.AreEqual(true, sut.UpdateRequired);
        }

        [TestMethod]
        public void VerifyFileDoesNotExistsDataIsGeneratedWithCorrectSequence()
        {
            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("123", "category");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("234", "category");
            Assert.AreEqual(2, value);
        }

        [TestMethod]
        public void VerifyLoadAndSave()
        {
            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("123", "categoryForSave1");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("234", "categoryForSave1");
            Assert.AreEqual(2, value);

            sut.Save();

            sut = new XmlUtils(testdir);
            value = sut.GetIdForValue("234", "categoryForSave1");
            Assert.AreEqual(2, value);
        }

        [TestMethod]
        public void VerifyLoadAndSaveNoUpdateRequired()
        {
            XmlUtils sut = new XmlUtils(testdir);
            sut.GetIdForValue("123", "categoryForSave1");
            sut.GetIdForValue("234", "categoryForSave1");

            sut.Save();

            sut = new XmlUtils(testdir);
            sut.GetIdForValue("234", "categoryForSave1");
            Assert.AreEqual(false, sut.UpdateRequired);
        }

        [TestMethod]
        public void VerifyLoadAndSaveUpdateRequiredWhenAddNewElement()
        {
            XmlUtils sut = new XmlUtils(testdir);
            sut.GetIdForValue("123", "categoryForSave1");
            sut.GetIdForValue("234", "categoryForSave1");

            sut.Save();

            sut = new XmlUtils(testdir);
            sut.GetIdForValue("xxxxxx", "categoryForSave1");
            Assert.AreEqual(true, sut.UpdateRequired);
        }

        [TestMethod]
        public void VerifySaveMultiple()
        {
            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("123", "categoryForSave2");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("234", "categoryForSave2");
            Assert.AreEqual(2, value);
            value = sut.GetIdForValue("xxx", "categoryForSave3");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("333", "categoryForSave3");
            Assert.AreEqual(2, value);
            sut.Save();

            sut = new XmlUtils(testdir);
            value = sut.GetIdForValue("234", "categoryForSave2");
            Assert.AreEqual(2, value);
            value = sut.GetIdForValue("333", "categoryForSave3");
            Assert.AreEqual(2, value);
        }

        [TestMethod]
        public void VerifyFileDoesNotExistsDataIsGeneratedWithCorrectSequenceAndLookup()
        {
            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("123", "category");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("123", "category");
            Assert.AreEqual(1, value);
        }

        [TestMethod]
        public void VerifyFileDoesNotExistsDataLookupInCategory()
        {
            XmlUtils sut = new XmlUtils(testdir);
            Int32 value = sut.GetIdForValue("123", "category");
            Assert.AreEqual(1, value);
            value = sut.GetIdForValue("234", "category");
            Assert.AreEqual(2, value);
            value = sut.GetIdForValue("123", "category");
            Assert.AreEqual(1, value);
        }
    }
}
